package com.imaker.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.imaker.domain.UserInfo;
import com.imaker.service.CacheService;
import com.imaker.service.UserService;
import com.imaker.utils.CacheUtils;
import com.imaker.vo.CacheInfo;
import com.opensymphony.xwork2.ActionContext;

 /**
 * <p>程序名称：       LoginAction.java</p>
 * <p>程序说明：       用户登录action</p>
 * <p>版权信息：       Copyright mayonghua</p>
 * 
 * @author：          mayonghua
 * @version：         Ver 0.1
 */
@Controller
@Scope("prototype")
public class LoginAction extends BaseAction {

	private String userName;
	private String password;
	private String error;
	
	@Autowired
	private UserService userService;
	@Autowired
	private CacheService cacheService;
	/**
	 * 用户登录判断
	 * @return String 登录成功状态
	 */
	public String login() throws Exception{
		UserInfo user = userService.findByProperty(userName.trim(), password.trim());
		if(user==null){
			error = "1";
			return "LOGINERROR";
		}
		//用户登录成功，设置缓存
		ActionContext.getContext().getSession().put("user",user);
		//如果缓存中已经有了，则不再保存
		//if(CacheUtils.get("ROLEOPERATE", user.getId()) == null){
		cacheService.refreshCache(user.getId());
		//}
		return SUCCESS;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public CacheService getCacheService() {
		return cacheService;
	}
	public void setCacheService(CacheService cacheService) {
		this.cacheService = cacheService;
	}
	
}
